package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysMenu;
import cn.knightzz.service.SysMenuService;
import cn.knightzz.util.JwtUtils;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ObjectOutput;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-04-18
 */
@RestController
public class SysMenuController {

    @Resource
    SysMenuService menuService;

    // 获取树形菜单

    @GetMapping("/menus/{id}")
    public JsonResult<List<SysMenu>> getTreeMenus(@PathVariable Integer id) {
        // 根据角色id 从 角色菜单表获取所有的菜单数据
        List<SysMenu> treeMenu = menuService.getTreeMenu(id);
        // 封装返回数据
        JsonResult<List<SysMenu>> result;
        if (treeMenu.isEmpty()) {
            result = new JsonResult<>("201", "菜单获取失败");
        } else {
            result = new JsonResult<>("200", "菜单获取成功");
            result.setData(treeMenu);
        }
        return result;
    }

    @GetMapping("/menus/list")
    public JsonResult<List<SysMenu>> getAllMenusByTree() {
        // 根据角色id 从 角色菜单表获取所有的菜单数据
        List<SysMenu> treeMenu = menuService.getAllMenuByTree();
        // 封装返回数据
        JsonResult<List<SysMenu>> result = new JsonResult<>("200", "菜单获取成功");
        result.setData(treeMenu);
        return result;
    }

    @PostMapping("/menus")
    public JsonResult<Object> addMenu(@RequestBody SysMenu menu){
        Boolean status = menuService.addMenus(menu);
        JsonResult<Object> result;
        if(status){
            result = new JsonResult<>("200" , "菜单添加成功");
        }else {
            result = new JsonResult<>("201" , "菜单添加失败");
        }
        return result;
    }

    @PutMapping("/menus")
    public JsonResult<Object> updateMenu(@RequestBody SysMenu menu){
        Boolean status = menuService.updateMenu(menu);
        JsonResult<Object> result;
        if(status){
            result = new JsonResult<>("200" , "菜单更新成功");
        }else {
            result = new JsonResult<>("201" , "菜单更新失败");
        }
        return result;
    }

    @DeleteMapping("/menus/{id}")
    public JsonResult<Object> deleteMenu(@PathVariable Long id){
        Boolean status = menuService.deleteMenu(id);
        if(status){
            return new JsonResult<>("200" , "菜单删除成功");
        }else{
            return new JsonResult<>("201" , "菜单删除失败");
        }
    }

}

